<?php
/**
 * 应聘信息
 */
namespace app\user\controller;

class Jobapply extends base
{
//前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();

            if (isset($data['status']) || !empty($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
        $this->datas ['uid'] = $this->getLoginUser()->id;
    }


    public function edit()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['status'] = 0;
            $data['uid'] = $this->getLoginUser()->id;
            $data['uname'] = $this->getLoginUser()->name;
            $validate = $data['id'] ? 'Purchase.edit' : 'Purchase.add';
            $result = $this->validate($data, $validate);
            if (true !== $result) {
                return ajax_info($result);
            }

            if (!empty($data['id'])) {
                return $this->update($data);
            }

            $result = $this->model->save($data);
            if ($result) {
                return ajax_info(lang('Submit successfully awaiting review'), 'y');

            }
            return ajax_info(lang('Submission Failed'));

        }
        $data = $this->model->where('id', $this->request->param('id'))
            ->where('uid', $this->getLoginUser()->id)
            ->find();
        $url= json_decode($data['url']);
        echo $url['title'];
        $this->assign(['data' => $data]);
        return $this->fetch();
    }


    public function Jobapplysave()
    {
        //申请职位
        if(!$this->getLoginUser()){
            return ajax_info(lang('Please login or register'),'y',url('user/index/user'));
        }
        $data = $this->request->param();

       $data['uid'] = $this->getLoginUser()->id;
        $dd = $this->model->isUpdate(true)
            ->save(['status' => '0'], ['jid' => $data['jid'],'navid'=>$data['navid'],'uid' => $data['uid']]);
        if ($dd) {
            return ajax_info(lang('Has resubmitted job application information'), 'u');
        }
        $data['lang']=cookie('think_var');
        $res =$this->model->isUpdate(false)
            ->allowField(true)
            ->save($data);
        if ($res) {
            return ajax_info(lang('Has submitted job application information'), 'u');
        } else {
            return ajax_info(lang('Failed to submit data'));
        }
    }
}
